package org.javaymw.springboot.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

import org.javaymw.springboot.base.BaseController;
import org.javaymw.springboot.dto.OrganizationDto;
import org.javaymw.springboot.dto.TreeOrganizationDto;
import org.javaymw.springboot.service.OrganizationService;
import org.javaymw.springboot.util.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/11/2 17:20
 * @Description: 组织机构管理
 */
@Controller
@RequestMapping("/admin/organization")
public class AdminOrganizationController extends BaseController {

    @Autowired
    private OrganizationService organizationService;

    /**
     * 获取组织树结构
     * @return
     */
    @GetMapping(value = "/tree")
    @ResponseBody
    public Map<String, Object> tree() {
        Map<String, Object> retMap = null;
        try {
            List<TreeOrganizationDto> organizationTree = organizationService.findOrganizationByTree();
            if (organizationTree != null) {
                retMap = MessageUtils.success();
                retMap.put("organizationTree", organizationTree);
                return retMap;
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     *
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(@RequestParam(value="pageNum", defaultValue="1") int pageNum,
                       @RequestParam(value="pageSize", defaultValue="10") int pageSize,
                       @RequestParam(value="parentId", defaultValue="0") Long parentId,
                       Model model) {
        Page<OrganizationDto> organizationDtos = organizationService.getOrganizationByPage(pageNum, pageSize, parentId);
        PageInfo<OrganizationDto> page = new PageInfo<>(organizationDtos);
        model.addAttribute("page", page);
        return "admin/organization/organizationList";
    }
}
